<template>
	<div id="activityDetails" class="activityDetails">
		<div>
			<form-preview v-if="showButtonUnderPendingConfirm" :header-label="activityName" :header-value="activityValue" :body-items="list" :footer-buttons="buttonsStep1" name="activityDetails">
			</form-preview>

			<form-preview v-else-if="showFeedback" :header-label="activityName" :header-value="activityValue" :body-items="list" :footer-buttons="buttonsStep2" name="activityDetails">
			</form-preview>

			<form-preview v-else-if="showFeedbackAction" :header-label="activityName" :header-value="activityValue" :body-items="list" :footer-buttons="buttonsStep3" name="activityDetails">
			</form-preview>

			<form-preview v-else :header-label="activityName" :header-value="activityValue" :body-items="list" name="activityDetails">
			</form-preview>

			<div v-transfer-dom>
				<confirm v-model="show"
				         :title="title"
				         @on-cancel="onCancel"
				         @on-confirm="onConfirm"
				         @on-show="onShow"
				         @on-hide="onHide">
					<p style="text-align:center;">{{ message }}</p>
				</confirm>
			</div>

			<div class="viewPopup" v-transfer-dom>
				<popup v-model="showCustomerList" height="270px" is-transparent>
					<div style="width: 60%;background-color:#fff;margin:0 auto;">
						<group>
							<cell class="cellViewPopup" v-for="item in customerList" :title="item.name" @click.native="jumpTo(item.id)" is-link></cell>
						</group>
					</div>
				</popup>
			</div>

		</div>
	</div>
</template>

<script>
	import bus from '@/event/eventBus'
	import NetWorking from '../../../../utils/networking'
	import PickList from '../../../../components/mobile/picklist/picklist'
	import {Popup, Search, Checklist, AlertModule, FormPreview, XButton, Flexbox, FlexboxItem, Group, Cell, TransferDomDirective as TransferDom, Confirm} from 'vux'
	export default {
		name: "pocustomer-details",
		directives: {
			TransferDom
		},
		components: {
			Popup,
			Search,
			Checklist,
			AlertModule,
			XButton,
			FormPreview,
			Flexbox,
			FlexboxItem,
			Group,
			Cell,
			TransferDom,
			Confirm,
			PickList
		},
		data() {
			return {
				showCustomerList: false,
				showPopup: false,
				btnClickText: '刷课时',
				btnDisable: false,
				currentMethod: '',
				title: '操作提示',
				show: false,
				message: '',
				showButtonUnderPendingConfirm: false,
				showFeedback: false,
				showFeedbackAction: false,
				customerList: [],
				activityModel: '',
				obj: '',
				id: '',
				lesson_id: '',
				activityName: '活动名称',
				activityValue: '',
				activityContent: '活动内容',
				attendees: '',
				list: [{
					label: '时间',
					value: ''
				},{
					label: '教师',
					value: ''
				}, {
					label: '学生',
					value: ''
				}],
				buttonsStep1: [{
					style: 'primary',
					text: '调整',
					onButtonClick: () => {
						this.$router.push({name: "updateLesson", params: {id: this.id}});
					}
				}, {
					style: 'primary',
					text: '丢弃',
					onButtonClick: () => {
						this.onClick('errorResolve')
					}
				}, {
					style: 'primary',
					text: '刷课',
					onButtonClick: () => {
						this.onClick('completeLesson')
					}
				}],
				buttonsStep2: [{
					style: 'primary',
					text: '写反馈',
					onButtonClick: () => {
						this.writeFeedback(this.customerList, this.activityModel)
					}
				}],
				buttonsStep3: [{
					style: 'primary',
					text: '查看反馈',
					onButtonClick: () => {
						this.viewFeedback(this.customerList, this.activityModel)
					}
				}]
			}
		},
		watch: {
			customerList: function () {
				for(var i=0; i<this.customerList.length; i++){
					//this.isFeedbackDone(this.customerList[i].id, this.id)
				}
			}
		},
		created: function () {
			bus.$emit("barController", 1)
			bus.$emit("setTopSimpleTitle", '活动详情')

			this.activityValue = this.$route.params.title
			this.activityContent = this.$route.params.content

			let _this = this
			NetWorking.doGet(
				'activities/getActivityModel',
				null,
				{
					params: {
						id: this.$route.params.id
					}
				}
			).then(
				response => {
					let rs = response.data
					if(rs.error_code == 0){
						if(rs.activity_model.sysActivity.status == 0 && this.ownedByCurrentUser(rs.activity_model.sysUserList)){
							_this.showButtonUnderPendingConfirm = true
						}else if(rs.activity_model.sysActivity.status == 1 && this.ownedByCurrentUser(rs.activity_model.sysUserList)){
							_this.showFeedback = true
							_this.customerList = rs.activity_model.sysCustomerList
							_this.activityModel = rs.activity_model
						}else if(rs.activity_model.sysActivity.status == 2 && this.ownedByCurrentUser(rs.activity_model.sysUserList)){
							_this.showFeedbackAction = true
							_this.customerList = rs.activity_model.sysCustomerList
							_this.activityModel = rs.activity_model
						}
						_this.id = rs.activity_model.sysActivity.id
						_this.activityValue = rs.activity_model.type.value
						_this.list[0].value = rs.activity_model.sysActivity.time
						_this.list[1].value = _this.userListToString(rs.activity_model.sysUserList)
						_this.list[2].value = _this.customerListToString(rs.activity_model.sysCustomerList)
						_this.lesson_id = rs.activity_model.type.id
					}else{
						alert(rs.error_message);
					}
				}
			)
		},
		methods: {
			jumpTo: function (id) {
				let _this = this
				NetWorking.doGet(
					'teachers/customer_act_feedback/getFeedbackIdWithCustomerAndActId',
					null,
					{
						params: {
							customer_id: id,
							activity_id: this.id
						}
					}
				).then(
					response => {
						let rs = response.data
						if(rs.error_code == 0){
							if(rs.id == null){
								alert('查询失败，没有这个反馈！')
							}else{
								_this.$router.push({ path: "/studentReport", query: { id: rs.id }})
							}
						}else{
							alert(rs.error_message)
						}
					}
				)
			},
			onCancel () {
				console.log('on cancel')
			},
			onConfirm (msg) {
				switch(this.currentMethod) {
					case 'errorResolve':
						this.errorResolve()
						break
					case 'completeLesson':
						this.completeLesson()
						break
					default:
						break
				}
			},
			onHide () {
				console.log('on hide')
			},
			onShow () {
				console.log('on show')
			},
			onClick: function(value) {
				switch (value) {
					case 'errorResolve':
						this.currentMethod = 'errorResolve'
						this.message = '确定标记为丢弃吗？'
						this.show = true
						break
					case 'completeLesson':
						this.currentMethod = 'completeLesson'
						this.show = true
						this.message = '确定要刷课吗？'
						break
					default:
						break
				}
			},
			viewFeedback(customerList, activityModel) {
				if(customerList.length > 1){
					this.showCustomerList = true
				}else if(customerList.length == 1){
					this.jumpTo(customerList[0].id)
				}else{
					alert('出错了，请重试！')
				}
			},
			writeFeedback(customerList, activityModel) {
				this.$router.push({name: "lessonFeedback", params: {customerList, activityModel}});
			},
			ownedByCurrentUser(userList){
				for(var i=0; i<userList.length; i++){
					if(userList[i].username == this.$store.state.user.userModel.sysUser.username){
						return true
					}
				}
				return false
			},
			errorResolve(){
				let _this = this
				NetWorking.doPatch(
					'activities/'+this.id,
					null,
					{
						status: 3
					},
					null
				).then(
					response => {
						let rs = response.data
						if(rs.error_code == 0){
							this.$store.dispatch('createPendingActNum', rs.pendingActNum)
							_this.$router.push({ name: 'lesson' , params: {page: 'getPendingConfirm'}})
						}else{
							alert(rs.error_message)
						}
					}
				)
			},
			completeLesson(){
				this.$vux.loading.show({
					text: '处理中...'
				})

				let _this = this
				NetWorking.doPost(
					'activities/completeLesson',
					null,
					null,
					{
						params:{
							id: this.id,
							lesson_id: this.lesson_id,
							lessonType: this.activityValue,
							time: this.list[0].value,
							teacher: this.list[1].value
						}
					}
				).then(
					response => {
						this.$vux.loading.hide()
						let rs = response.data
						if(rs.error_code == 0){
							_this.$store.dispatch('createPendingActNum', rs.pendingActNum)
							_this.showModule()
						}else{
							alert(rs.error_message)
						}
					}
				)
			},
			showModule () {
				let _this = this
				AlertModule.show({
					title: '恭喜',
					content: '刷课成功',
					onShow () {
						console.log('Module: I\'m showing')
					},
					onHide () {
						_this.$router.push({ name: 'lesson' , params: {page: 'getPendingConfirm'}})
					}
				})
			},
			userListToString(list){
				var str = ''
				for(var i=0; i<list.length; i++){
					str += list[i].nickname + ','
				}
				str = str.substr(0, str.length-1)
				return str
			},
			customerListToString(list){
				var str = ''
				for(var i=0; i<list.length; i++){
					str += list[i].name + ','
				}
				str = str.substr(0, str.length-1)
				return str
			}
		}
	}
</script>

<style lang="less">
	@import '~vux/src/styles/close.less';

	.close{
		text-align: right;
		padding-right: 5px;
	}

	.activityDetails {
		margin-top: 46px;
	}

	.pickListPopup {
		margin-top: 56px;
		width:100%;
		height:100%;
	}

	.viewPopup > .vux-popup-dialog {
		bottom: auto !important;
	}

	.cellViewPopup > .vux-cell-bd > p > .vux-label {
		color: lightskyblue;
	}
</style>